package com.jiulan.hearthearing.ui.course.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseBottomDialog;
import com.jiulan.hearthearing.ui.course.activity.CourseDetailsFMActivity;
import com.jiulan.hearthearing.ui.course.adapter.CoursePlaySpeedSetAdapter;

import java.util.ArrayList;
import java.util.List;

public class CoursePlayTimingSetDialog extends BaseBottomDialog {
    private Context context;
    private String choose = "";
    private List<String> data = new ArrayList<>();
    private CoursePlaySpeedSetAdapter speedSetAdapter;

    private RecyclerView mRecyclerView;
    private TextView tvCancel;

    public CoursePlayTimingSetDialog(Context context, String choose) {
        this.context = context;
        this.choose = choose;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_course_fm;
    }

    @Override
    public void bindView(View v) {
        mRecyclerView = v.findViewById(R.id.mRecyclerView);
        tvCancel = v.findViewById(R.id.tvCancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        data.add("不开启");
        data.add("播完当前节目");
        data.add("播完2集节目");
        data.add("播完3集节目");
        data.add("10分钟");
        data.add("20分钟");
        data.add("30分钟");
        speedSetAdapter = new CoursePlaySpeedSetAdapter(data);
        speedSetAdapter.setClickPosition(choose);
        mRecyclerView.setAdapter(speedSetAdapter);
        speedSetAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                speedSetAdapter.setClickPosition(data.get(position));
                speedSetAdapter.notifyItemChanged(position);
                sendBroadcast(4, data.get(position));
                dismiss();
            }
        });
    }

    private void sendBroadcast(int action, String speed) {
        Intent intent = new Intent(CourseDetailsFMActivity.LOCAL_BROADCAST);
        intent.putExtra("action", action);
        intent.putExtra("timing", speed);
        CourseDetailsFMActivity.localBroadcastManager.sendBroadcast(intent);
    }
}
